﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="EditInvoiceDetailsCtrl.ascx.cs" Inherits="WebUI.Controls.EditInvoiceDetailsCtrl" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<script type="text/javascript">
    function roundNumber(num, dec) {
	    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	    return result;
    }
    
    var sysGST = <%= SysVars.GST %>;
    function updateTotalAmt() {
        var unitPrice = document.getElementById("<%=txtUnitPrice.ClientID %>").value;
        if (unitPrice == '') unitPrice = 0;

        var qty = document.getElementById("<%=txtQuantity.ClientID %>").value;
        if (qty == '') qty = 1;
        if(qty == '0')
        {
            alert("Quantity = 0 is not allowed!");
            qty = 1;
            document.getElementById("<%=txtQuantity.ClientID %>").value = qty;
        }
        
        var gst = document.getElementById("<%=txtGST.ClientID %>").value;
        if (gst == '') gst = 0;
        
        var isPercentage = document.getElementById("<%=chkUsePercentage.ClientID %>").checked;
        var discount = document.getElementById("<%=txtDiscount.ClientID %>").value;
        if (discount == '') discount = 0;

        var total = 0;
        if(isPercentage)
            total = roundNumber(unitPrice * qty * (100 - discount) / 100, 2);
        else
            total = unitPrice * qty - discount;
        
        document.getElementById("<%=txtTotal.ClientID %>").value = total;
        
        if(gst > 0){
            gst = roundNumber(total * sysGST / (sysGST + 100), 2);
            document.getElementById("<%=txtGST.ClientID %>").value = gst;
        }
    }
    
    function onLostFocus(txt){        
        if(txt.value == ''){
            txt.value = '0';
        }
    }
    
    function onQtyLostFocus(txt){        
        var qty = txt.value;
        if (qty == '') qty = 1;
        if(qty == '0')
        {
            alert("Quantity = 0 is not allowed!");
            qty = 1;
            txt.value = qty;
        }
    }

    function onUnitPriceLostFocus(txt){
        var qty = txt.value;
        if (qty == '') qty = 0;
        txt.value = qty;
    }

</script>

<asp:Panel ID="mainPanel" runat="server">
    <table>
        <tr>
            <td style="text-align:right; vertical-align:top">
                <asp:Label ID="label1" runat="server" Text="Product:" class="labelMessage"></asp:Label>
            </td>
            <td style="text-align:left; vertical-align:top">
                <asp:TextBox BackColor="#b5d4ff"  ReadOnly="true" ID="txtProductName" Width="200px" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="text-align:right; vertical-align:top">
                <asp:Label ID="label2" runat="server" Text="Unit Price:" class="labelMessage"></asp:Label>
            </td>
            <td style="text-align:left; vertical-align:top">
                <asp:TextBox BackColor="#b5d4ff" onfocus="this.select();" ReadOnly="true" onchange="updateTotalAmt();" onblur="onUnitPriceLostFocus(this);" ID="txtUnitPrice" Width="100px" runat="server"></asp:TextBox>
                <cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender3" TargetControlID="txtUnitPrice" FilterType="Numbers,Custom" ValidChars=".-" runat="server">
                </cc1:FilteredTextBoxExtender>
            </td>
        </tr>
        <tr>
            <td style="text-align:right; vertical-align:top">
                <asp:Label ID="lblUnitCost" runat="server" Text="Unit Cost:" class="labelMessage"></asp:Label>
            </td>
            <td style="text-align:left; vertical-align:top">
                <asp:TextBox BackColor="#b5d4ff"  ReadOnly="true" ID="txtUnitCost" Width="100px" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="text-align:right; vertical-align:top">
                <asp:Label ID="label3" runat="server" Text="Quantity:" class="labelMessage"></asp:Label>
            </td>
            <td style="text-align:left; vertical-align:top">
                <asp:TextBox ID="txtQuantity" onfocus="this.select();" Width="100px" runat="server" onchange="updateTotalAmt();" onblur="onQtyLostFocus(this);" ></asp:TextBox>
                <cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" TargetControlID="txtQuantity"  FilterType="Numbers"  runat="server">
                </cc1:FilteredTextBoxExtender>
            </td>
        </tr>
        <tr>
            <td style="text-align:right; vertical-align:top">
                <asp:Label ID="label4" runat="server" Text="Discount:" class="labelMessage"></asp:Label>
            </td>
            <td style="text-align:left; vertical-align:top">
                <asp:TextBox BackColor="#b5d4ff"  ID="txtDiscount" onfocus="this.select();" Width="100px" runat="server" onchange="updateTotalAmt();" onblur="onLostFocus(this);" ></asp:TextBox>
                <asp:CheckBox ID="chkUsePercentage" runat="server" Text="Use Percentage(%)" onclick=' updateTotalAmt();' />
                <cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender2" TargetControlID="txtDiscount"  FilterType="Numbers,Custom" ValidChars="." runat="server">
                </cc1:FilteredTextBoxExtender>
            </td>
        </tr>
        <tr>
            <td style="text-align:right; vertical-align:top">
                <asp:Label ID="label5" runat="server" Text="GST:" class="labelMessage"></asp:Label>
            </td>
            <td style="text-align:left; vertical-align:top">
                <asp:TextBox BackColor="#b5d4ff"  ReadOnly="true" ID="txtGST" Width="100px" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="text-align:right; vertical-align:top">
                <asp:Label ID="label6" runat="server" Text="Total Amount:" class="labelMessage"></asp:Label>
            </td>
            <td style="text-align:left; vertical-align:top">
                <asp:TextBox BackColor="#b5d4ff"  ReadOnly="true" ID="txtTotal" Width="100px" runat="server"></asp:TextBox>
            </td>
        </tr>
    </table>
</asp:Panel>